package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.mapper.DtsGoodsProductMapper;
import com.qf.pojo.DtsGoodsAttribute;
import com.qf.pojo.DtsGoodsProduct;
import com.qf.service.DtsGoodsProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/24 14:36
 */
@Service
public class DtsGoodsProductServiceImpl implements DtsGoodsProductService {
    @Autowired
    private DtsGoodsProductMapper dtsGoodsProductMapper;
    @Override
    public List<DtsGoodsProduct> selectByGoodsId(Integer id) {
        return dtsGoodsProductMapper.selectList(new QueryWrapper<DtsGoodsProduct>().eq("goods_id",id).eq("deleted",false));
    }

    @Override
    public void update(List<DtsGoodsProduct> products ,Integer id) {
        List<DtsGoodsProduct> goodsProduct = dtsGoodsProductMapper.selectList(new QueryWrapper<DtsGoodsProduct>().eq("goods_id", id).eq("deleted",false));
        ArrayList<DtsGoodsProduct> insertGoodsProducts  = new ArrayList();
        for (DtsGoodsProduct product : products) {
           if(!goodsProduct.contains(product)){
               product.setGoodsId(id);
               insertGoodsProducts.add(product);
           }
        }
         dtsGoodsProductMapper.insertList(insertGoodsProducts);
        ArrayList<DtsGoodsProduct> deleteGoodsProducts = new ArrayList<>();
        for (DtsGoodsProduct dtsGoodsProduct : goodsProduct) {
            if(!products.contains(dtsGoodsProduct)){
                deleteGoodsProducts.add(dtsGoodsProduct);
            }
        }
        dtsGoodsProductMapper.updateList(products);
    }

    @Override
    public void insert(List<DtsGoodsProduct> products) {
   dtsGoodsProductMapper.insertList((ArrayList<DtsGoodsProduct>) products);
    }
}
